package top.ishowcode.bootweb01.controller;

import org.springframework.web.bind.annotation.*;
import top.ishowcode.bootweb01.bean.User;

import java.util.Map;

@RestController
public class ParameterTestController {
    //路径上带参数还有请求头,cookie等一些参数
    @GetMapping("/getuser/{id}/{name}/{addr}")//http://localhost:8080/getuser/1/tian/HangZhou
    public User getUser(@PathVariable("id") Integer id, @PathVariable("name") String name, @PathVariable("addr") String addr, @PathVariable Map<String,String> map,
                        @RequestHeader("User-Agent") String userAgent,@RequestHeader Map<String,String> headers){
        User user1 = new User();
        user1.setId(id);
        user1.setName("Tian");
        user1.setAddr(addr);
        //通过map封装整个路径参数
        User user2 = new User();
        user2.setId(Integer.parseInt(map.get("id")));
        user2.setName(map.get("name"));
        user2.setAddr(map.get("addr"));
        System.out.println("useragent="+userAgent);
        System.out.println("userHost="+headers.get("host"));
        System.out.println("userPlatform="+headers.get("sec-ch-ua-platform"));
        System.out.println(headers);
        return user2;
    }
    //两种可以混合http://localhost:8080/getuser2/1/tian/HangZhou?addr=Hangzhou&id=1&name=Tian
    //表单提交得到表单的数据用@RequestBody
    @GetMapping("/getuser2")//http://localhost:8080/getuser2?addr=Hangzhou&id=1&name=Tian
    public User getUser2(@RequestParam("id") Integer id,@RequestParam("name") String name,@RequestParam("addr") String addr,@RequestParam Map<String,String> map,@RequestBody String content){
        User user = new User();
        user.setAddr(addr);
        user.setId(id);
        user.setName(name);
        User user1 = new User();
        user1.setAddr(map.get("addr"));
        user1.setId(Integer.parseInt(map.get("id")));
        user1.setName(map.get("name"));
        return user;
    }

}
